import {View } from "@tarojs/components";
import AssetImage from "@/common/AssetImage";
import DynamicForm, {FormConfig} from "@/components/DynamicForm";
import {teamSaveApi} from "@/common/apis/home_datasource";
import Utils from "@/utils/common";
import {useEffect, useRef} from "react";
import Taro from "@tarojs/taro";

definePageConfig({
  navigationBarTitleText: '创建课题组'
})
export default function ResearchTeamCreate() {
  // 表单配置
  const rechargeFormConfigs: FormConfig[] = [
    {
      type: 'input',
      name: 'name', // Logical name for recipient name
      label: '课题组名称',       // Label text (without *)
      placeholder: '请填写课题组名称',
      layout: 'horizontal',
      required: true,        // Has asterisk
      rules: []
    },
    {
      type: 'textarea',
      name: 'note',
      label: '备注(选填)',
      layout: 'vertical',
      placeholder: '请填写备注',
      minHeight: '200px',
      maxlength: 200,
      inputBgColor: AssetImage.bg,
      showWordLimit: true
    },

  ];

  const eventChannel = useRef<Taro.EventChannel | null>()

  useEffect(() => {
    eventChannel.current = Utils.getChannel(Utils.getI())
  }, []);


  // 提交回调
  const handleSubmit = (values) => {
    console.log('Form values:', values);
    teamSaveApi(values).then((res) => {
      if (res) {
        Utils.showOkToast('创建成功');
        if (eventChannel.current) {
          Utils.emit(eventChannel.current, 'teamAdded', true);
        }
        Utils.navigateBack()
      }
    })
    // 处理表单提交逻辑
  };

  // 错误回调
  const handleError = (errors) => {
    console.error('Form errors:', errors);
  };

  return (
    <View>
      <DynamicForm
        formConfigs={rechargeFormConfigs}
        onSubmit={handleSubmit}
        onError={handleError}
        submitText='创建课题组'
      />
    </View>
  );
}


